package com.lmk;

import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.*;

/**
 * 自定义的面板
 * @author LaoMake
 * @email laomake@hotmail.com
 */
public class MyFrame {

    // 界面窗体
    private JFrame jFrame;

    public void init(){
        // 创建窗体
        jFrame = new JFrame("超级工具箱");

        // 设置布局方式
        jFrame.setLayout(new GridLayout(4, 1));

        // 设置风格样式
        try {
            String look = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; // Windows风格
            // String look = "javax.swing.plaf.metal.MetalLookAndFeel"; // Meta默认
          // String look = "javax.swing.plaf.nimbus.NimbusLookAndFeel"; // nimbus 水晶风格
            // String look = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"; // Windows Classic风格
            // String look = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; // Motif风格
            // String look = "com.sun.java.swing.plaf.mac.MacLookAndFeel"; // Mac风格，需要Mac操作系统
            // String look = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; // GTK风格，需要相关操作系统

            UIManager.setLookAndFeel(look);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 设置关闭窗口时退出程序
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 设置组件
        setComponents();

        // 打包
        jFrame.pack();

        // 设置尺寸
        jFrame.setSize(600, 600);

        // 设置居中显示
        jFrame.setLocationRelativeTo(null);

        // 显示窗体
        jFrame.setVisible(true);
    }

    /**
     * 设置组件
     */
    private void setComponents(){
        // 顶部标题
        JPanel topPanel = new JPanel();
        JLabel headerLabel = new JLabel("欢迎使用超级工具箱");
        headerLabel.setFont(new Font(Font.MONOSPACED, Font.BOLD, 22));
        topPanel.add(headerLabel, BorderLayout.CENTER);
        jFrame.add(topPanel);

        // 功能面板
        JPanel actionPanel = new JPanel();
        // 记事本按钮
        JButton notePadBtn = new JButton("打开记事本");
        notePadBtn.setFocusPainted(false);
        notePadBtn.addActionListener(e -> {
            Cmd.system("notepad");
        });
        actionPanel.add(notePadBtn);

        // 计算器按钮
        JButton calcBtn = new JButton("打开计算器");
        calcBtn.setFocusPainted(false);
        calcBtn.addActionListener(e -> {
            Cmd.system("calc");
        });
        actionPanel.add(calcBtn);

        // QQ按钮
        JButton qqBtn = new JButton("打开QQ");
        qqBtn.setFocusPainted(false);
        qqBtn.addActionListener(e -> {
            Cmd.system("D:\\Program Files\\Tencent\\QQ\\Bin\\QQScLauncher.exe");
        });
        actionPanel.add(qqBtn);
        jFrame.add(actionPanel);


        // 自定义功能面板
        JPanel diyPanel = new JPanel();

        // 文本输入框
        JTextField pathInput = new JTextField(60);

        // 文件选择框
        JFileChooser jFileChooser = new JFileChooser();

        // 文件选择按钮
        JButton chooseBtn = new JButton("选择");
        chooseBtn.addActionListener(e -> {
            int result = jFileChooser.showDialog(null, "选择文件");
            // 当用户正确选择文件后，获取文件路径
            if (result == JFileChooser.APPROVE_OPTION){
                String path = jFileChooser.getSelectedFile().getAbsolutePath();
                // 将文件路径设置到输入框
                pathInput.setText(path);
            }
        });

        // 运行按钮
        JButton runBtn = new JButton("运行");
        runBtn.addActionListener(e -> {
            Cmd.run(pathInput.getText());
        });


        diyPanel.add(pathInput);
        diyPanel.add(chooseBtn);
        diyPanel.add(runBtn);
        jFrame.add(diyPanel);

        //添加多行文本输入框
        JTextArea jTextArea = new JTextArea( 5, 55);JButton chooseFileBtn = new JButton( "选择文件");
        chooseFileBtn.addActionListener(e -> {
            int res =jFileChooser.showDialog(null,"选择软件");
            if(res == JFileChooser.APPROVE_OPTION){
                File file =jFileChooser. getSelectedFile();
                try {
                    Scanner sc= new Scanner(file);
                    while(sc.hasNext()){
                        String text =jTextArea.getText();
                        text=text +"\n"+ sc.nextLine();
                                jTextArea.setText(text);
                    }
                }catch (FileNotFoundException fileNotFoundException) {
                    fileNotFoundException.printStackTrace();
                }
            }});
        JPanel textPanel = new JPanel();
        textPanel.add(jTextArea);
        textPanel.add(chooseFileBtn);
        jFrame.add(textPanel);


    }
}